﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComLib;
using ComLib.Data;
using ComLib.Web.Modules.Widgets;


namespace ComLib.Web.Modules.Helpers
{
    public class WidgetHelper
    {
        /// <summary>
        /// Iterate over each widget/instance in the specified zone.
        /// </summary>
        /// <param name="zone"></param>
        /// <param name="executor"></param>
        public static void ForEachInZone(string zone, Action<WidgetInstance> executor)
        {
            var widgets = Widget.Repository.ToLookUp();
            var instances = WidgetInstance.Find(Query<WidgetInstance>.New().Where(w => w.Zone).Is(zone)
                                                                              .And(w => w.IsActive).Is(true)
                                                                              .OrderBy( w => w.SortIndex));
            foreach (var instance in instances)
            {
                var widget = widgets[instance.WidgetId];
                instance.Definition = widget;
                executor(instance);
            }
        }
    }
}
